package il.ac.tau.cvws.scrabblesidekick.server.db;

import il.ac.tau.cvws.scrabblesidekick.server.db.connection.MySQLConnection;
import il.ac.tau.cvws.scrabblesidekick.server.db.dao.LetterDao;
import il.ac.tau.cvws.scrabblesidekick.shared.datatype.game.Alphabet;
import il.ac.tau.cvws.scrabblesidekick.shared.datatype.game.Rack;
import il.ac.tau.cvws.scrabblesidekick.shared.datatype.game.Tile;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class DictionaryAlphabet 
{
	private static MySQLConnection myConnect = new MySQLConnection();

	/*
	 * LOGIC: the following function retrieves the alphabet and their values of the given dictionary
	 * INPUT: dictionaryId- identifier of the dictionary in DB
	 */
	public static Alphabet getAlphabet(int dictionaryId) throws SQLException
	{
		Connection connection = null;
		Statement statement   = null;
		Map<Integer, Integer> alephbet = new HashMap<Integer, Integer>();
		try
		{
			connection = myConnect.getConnection();

			statement = connection.createStatement();
			ResultSet rs = statement.executeQuery(" SELECT " + LetterDao.LETTER_ID + "," + LetterDao.VALUE  + 
												  " FROM "   + LetterDao.getName() + 
												  " WHERE "  + LetterDao.DICTIONARY_ID + " = " + dictionaryId);

			while (rs.next())
			{
				int letterID = rs.getInt(LetterDao.LETTER_ID);
				int value 	 = rs.getInt(LetterDao.VALUE);
				alephbet.put(letterID, value);
			}
		}
		catch(SQLException e)
		{
			throw e; 
		}
		finally
		{
			try
			{
				if (statement != null)  statement.close();
				if (connection != null) connection.close();
			}
			catch (SQLException e)
			{
				throw e; 
			}
		}
		return new Alphabet(alephbet);
	}

	/*
	 * LOGIC: the following function converts the tiles of the given rack to a "general rack". the general rack contains tiles which there index is their place in alphabet and
	 * their value in scrabble game. the conversion is needed because the user inserts the letters of his rack as Characters and the letters are mapped to integers in DB/
	 * INPUT: dictionaryId- identifier of the dictionary in DB, rack- the user's rack
	 * RETURN: the general rack
	 */
	public static Rack generalizeRack(Rack rack, int dictionaryId) throws SQLException 
	{
		List<Tile> generalRack = new ArrayList<Tile>(rack.rackLength());
		List<Tile> charRack = rack.getTiles();

		for(Tile tile: charRack)
		{
			generalRack.add(findCharacterIndex(tile.getIndex(), dictionaryId));
		}	
		
		return new Rack(generalRack);
	}

	private static Tile findCharacterIndex(int letter, int dictionaryId) throws SQLException 
	{
		Connection connection = null;
		Statement statement   = null;
		Tile tile 			  = null;
		
		try
		{
			connection = myConnect.getConnection();

			statement = connection.createStatement();
			ResultSet rs = statement.executeQuery(" SELECT " + LetterDao.LETTER_ID + "," + LetterDao.VALUE  + 
					  							  " FROM " + LetterDao.getName() + 
					  							  " WHERE " + LetterDao.LETTER_ID + " = " + letter + "" +
					  							  " AND " + LetterDao.DICTIONARY_ID + " = " + dictionaryId);
			while(rs.next())
			{
				int letterID = rs.getInt(LetterDao.LETTER_ID);
				int value 	 = rs.getInt(LetterDao.VALUE);
				tile = new Tile(letterID,value);
			}
		}
		catch(SQLException e)
		{
			throw e; 
		}
		finally
		{
			try
			{
				if (statement != null)  statement.close();
				if (connection != null) connection.close();
			}
			catch (SQLException e)
			{
				throw e; 
			}
		}
		return tile;
	}
}


